/**
 * 此处判断用户名或密码的正则
 */
+function(){
    $(".username").change(function(){

        let username = $(".username").val();
        let regUsername=/^[a-zA-Z][a-zA-Z0-9]{3,15}$/;    //正则用户名判断
        let regEmail=/^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;   //正则邮箱判断
        let regPhone=/^1\d{10}$/;                         //正则手机号判断

        if(username == ""){
            $(".username").next().html("请输入正确的用户名");
            return true;
        }
        if(/@+/.test(username)){
            if(regEmail.test(username)==false){
                $(".username").next().html("Email格式不正确，例如web@sohu.com");
                return false;
            }
            $(".username").next().html(" ");
            return true;
        }
        if(/^[0-9]*$/.test(username)){
            if(regPhone.test(username)==false){
                $(".username").next().html("手机号码不正确，请重新输入");
                return false;
            }
            $(".username").next().html(" ");
            return true;
        }
        if(/^[0-9a-zA-Z_]{1,}$/.test(username)){
            if(regUsername.test(username)==false){
                $(".username").next().html("安踏账号不正确");
                return false;
            }
            $(".username").next().html(" ");
            return true;
        }
    })    
    $(".password").change(function(){
        let password = $(".password").val();
        let reg=/^[a-zA-Z0-9]{4,10}$/;                  //正则密码判断
        if(reg.test(password)==false){
            $(".password").next().html("密码不能含有非法字符，长度在4-10之间");
            return false;
        }else{
            $(".password").next().html(" ");
            return true;
        }
    })    
}()
/**
 * 点击立即登录获取用户名和密码
 */
//引入xhrPost函数
function xhrPost(url,data){
    return new Promise((resolve,reject)=>{
          var xhr = new XMLHttpRequest();
          xhr.open("POST",url);
          xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
          var _data = [];
          for(let key in data){
                _data.push(`${key}=${data[key]}`);
          }
          xhr.send(_data.join("&"));
          xhr.onload = function(){
                xhr.status === 200 ? resolve(xhr.responseText) : reject(xhr.status);
          }
    })
}
//点击立即登录获取用户名和密码
+function(){
    $(".loginNow").click(function(){
        let username = $(".username").val();
        let password = $(".password").val();
        xhrPost("http://120.79.139.196/antaPhp/login.php",{
            username : username,
            password : password
        }).then(function(res){
            res = JSON.parse(res);
            console.log(res)
            if(res.statu === "success"){
                setTimeout(function(){
                    location.href = "./index.html";
                },3000)
            }
        })
    })
}()
// btn.onclick = function(){
// 	var usrValue = usr.value;
// 	var pwdValue = pwd.value;
// 	//http://localhost/GP10/0417/server/interface/login.php
// 	xhrPost("http://59.110.157.147/server/group7server/login.php",{
// 		username : usrValue,
// 		password : pwdValue
// 	}).then(function(res){
// 		res = JSON.parse(res);
// 		console.log(res)
// 		if(res.statu === "success"){
// 			setTimeout(function(){
// 				location.href = "./index.html";
// 			},3000)
// 		}
// 	})
// }